package com.easylinkin.linkappapi.device.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.entity.DeviceVoExt;
import com.easylinkin.linkappapi.device.service.DeviceRefAreaScopeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 设备关联功能关联作用区域 前端控制器
 * </p>
 *
 * @author TongJie
 * @since 2021-05-12
 */
@RestController
@Api(value="爱尔设备、cloud版空调设备接口",tags={"爱尔设备、cloud版空调设备接口"}, description="重新绑定空间区域，设备分组专用")
@RequestMapping("/device/deviceRefAreaScope")
@Slf4j
public class DeviceRefAreaScopeController {

    @Resource
    private DeviceRefAreaScopeService deviceRefAreaScopeService;

    /**
     * 根据区域查询条件获取设备列表--功能识别码过滤
     * @param requestModel
     * @return
     */
    @ApiOperation("根据区域查询条件获取设备 分页 数据--功能识别码过滤")
    @PostMapping("getPage")
    public RestMessage getPage(@RequestBody RequestModel<DeviceVoExt> requestModel){
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams().getFunctionIdentifier(), "功能识别码不能为空");

        IPage<Device> deviceListByAreaPathAndFunctionIdentifier = null;
        try {
            deviceListByAreaPathAndFunctionIdentifier = deviceRefAreaScopeService.getDeviceListByAreaPathAndFunctionIdentifier(requestModel.getPage(), requestModel.getCustomQueryParams());
        } catch (Exception e) {
            log.error("getDeviceListByAreaPathAndFunctionIdentifier 出现异常：{}", e);
            return RestBuilders.failureBuilder().message(e.getMessage()).build();
        }
        return RestBuilders.successBuilder().data(deviceListByAreaPathAndFunctionIdentifier).build();
    }

    @ApiOperation("根据区域查询条件获取设备 list 数据--功能识别码过滤")
    @PostMapping("getAll")
    public RestMessage getAll(@RequestBody DeviceVoExt deviceVoExt){
        Assert.notNull(deviceVoExt, "参数不能为空");
        Assert.notNull(deviceVoExt.getFunctionIdentifier(), "功能识别码不能为空");

        List<Device> deviceListByAreaPathAndFunctionIdentifier = null;
        try {
            deviceListByAreaPathAndFunctionIdentifier = deviceRefAreaScopeService.getDeviceListByAreaPathAndFunctionIdentifier(deviceVoExt);
        } catch (Exception e) {
            log.error("getDeviceListByAreaPathAndFunctionIdentifier 出现异常：{}", e);
            return RestBuilders.failureBuilder().message(e.getMessage()).build();
        }
        return RestBuilders.successBuilder().data(deviceListByAreaPathAndFunctionIdentifier).build();
    }

    /**
     * 绑定区域和设备关联
     * @param adp
     * @return
     */
    @ApiOperation("绑定区域和设备关联")
    @PostMapping("bindAreaPathAndDevice")
    public RestMessage bindAreaPathAndDevice(@RequestBody DeviceVoExt adp){
        Assert.notNull(adp.getFunctionIdentifier(), "功能识别码不能为空");
        try {
            deviceRefAreaScopeService.bindAreaPathAndDevice(adp);
        } catch (Exception e) {
            log.error("bindAreaPathAndDevice 出现异常：{}", e);
            return RestBuilders.failureBuilder().message(e.getMessage()).build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    /**
     * 一键同步绑定
     * @param adp
     * @return
     */
    @ApiOperation("一键同步绑定")
    @PostMapping("oneKeySynBind")
    public RestMessage oneKeySynBind(@RequestBody DeviceVoExt adp){
        Assert.notNull(adp.getFunctionIdentifier(), "功能识别码不能为空");
        try {
            deviceRefAreaScopeService.oneKeySynBind(adp);
        } catch (Exception e) {
            log.error("oneKeySynBind 出现异常：{}", e);
            return RestBuilders.failureBuilder().message(e.getMessage()).build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    /**
     * 解除绑定
     * @param adp
     * @return
     */
    @ApiOperation("解除绑定")
    @PostMapping("unbindDevice")
    public RestMessage unbindDeviceAndFunctionIdentifier(@RequestBody DeviceVoExt adp){
        Assert.notNull(adp.getFunctionIdentifier(), "功能识别码不能为空");
        try {
            deviceRefAreaScopeService.unbindDeviceAndFunctionIdentifier(adp);
        } catch (Exception e) {
            log.error("unbindDevice 出现异常：{}", e);
            return RestBuilders.failureBuilder().message(e.getMessage()).build();
        }
        return RestBuilders.successBuilder().message("成功").build();
    }

    /**
     * 导入设备关联区域
     * @param file
     * @param functionIdentifier
     * @return
     */
    @ApiOperation("导入设备关联区域")
    @PostMapping("importDevice")
    public RestMessage importDevice(@RequestParam(value = "file") MultipartFile file, @RequestParam("functionIdentifier") String functionIdentifier) {
        Assert.notNull(file, "文件不能为空");
        Assert.notNull(functionIdentifier, "功能识别字段不能为空");
        String[] result = deviceRefAreaScopeService.importDevice(file, functionIdentifier);
        if ("0".equals(result[0])) {
            return RestBuilders.successBuilder().data(result[1]).build();
        }
        return RestBuilders.failureBuilder().message(result[1]).build();
    }

    /**
     * 导出模板文件
     * @param request
     * @param response
     */
    @ApiOperation("导出模板文件")
    @GetMapping("exportTemplateFile")
    public void exportTemplateFile(HttpServletRequest request, HttpServletResponse response) {
        deviceRefAreaScopeService.exportAirDeviceTemplateFile(request, response);
    }

}
